/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Bean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import java.util.*;
import java.sql.*;
import ProjectMgtSystem.Utilities.*;
import ProjectMgtSystem.Entities.*;

/**
 * Engineer Action 
 * @author Nguyen Phu Cuong
 * Created: 2011/07/19
 */
public class EngineerActionBean extends org.apache.struts.action.ActionForm {

    private String name;
    private int number;
    private String engineerid;
    private String firstname;
    private String lastname;
    private String major;
    private String description;

    /**
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * @param string
     */
    public void setName(String string) {
        name = string;
    }

    /**
     * @return
     */
    public int getNumber() {
        return number;
    }

    /**
     * @param i
     */
    public void setNumber(int i) {
        number = i;
    }

    /**
     *
     */
    public EngineerActionBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getName() == null || getName().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;
    }

    /**
     * @return the engineerid
     */
    public String getEngineerid() {
        return engineerid;
    }

    /**
     * @param engineerid the engineerid to set
     */
    public void setEngineerid(String engineerid) {
        this.engineerid = engineerid;
    }

    /**
     * @return the firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * @return the lastname
     */
    public String getLastname() {
        return lastname;
    }

    /**
     * @param lastname the lastname to set
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    /**
     * @return the major
     */
    public String getMajor() {
        return major;
    }

    /**
     * @param major the major to set
     */
    public void setMajor(String major) {
        this.major = major;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * get arraylist of engineer
     */
    public ArrayList<Engineer> view() throws SQLException {
        ArrayList<Engineer> enList = new ArrayList<Engineer>();
        Conn cnn = new Conn();
        String sql = "select * from engineer where engineer.EDelete=0";
        ResultSet rs = cnn.exec(sql);
        while (rs.next()) {
            Engineer engineer = new Engineer();
            engineer.setEngineerID(rs.getString("EngineerID"));
            engineer.setFirstName(rs.getString("FirstName"));
            engineer.setLastName(rs.getString("LastName"));
            engineer.setMajor(rs.getString("Major"));
            engineer.setDescription(rs.getString("Description"));
            enList.add(engineer);
        }
        return enList;
    }

    /**
     * insert a new engineer record
     */
    public String insert(String engineerid, String firstname, String lastname, String major, String description) throws SQLException {
        //+++<Hien 07/25/2011: fix code>
        DBHelper.getInstance().initConnection();
        ArrayList<String> arrP= new ArrayList<String>();
        arrP.add(engineerid);
        arrP.add(firstname);
        arrP.add(lastname);        
        arrP.add(major);        
        arrP.add(description);
        String message = DBHelper.getInstance().executeUpdateProcReturn("Insert_Engineer", arrP);
        DBHelper.getInstance().closeConnection();
        
//        int row = 0;
//        Conn cnn = new Conn();
//        String sql = "insert into engineer(engineerid, firstname, lastname, major, description, edelete) values(?,?,?,?,?,?)";
//        PreparedStatement prstm = cnn.getPrepareStatement(sql);
//        prstm.setString(1, engineerid);
//        prstm.setString(2, firstname);
//        prstm.setString(3, lastname);
//        prstm.setString(4, major);
//        prstm.setString(5, description);
//        prstm.setInt(6, 0);
//        row = prstm.executeUpdate();
//        return row;
        //---<Hien 07/25/2011: fix code>
        return message;
    }

    /**
     * Delete
     */
    public String delete(String engineerid) throws SQLException {
      //+++<Hien 07/25/2011: fix code>
//        int row = 0;
//        Conn cnn = new Conn();
//        String sql = "delete from engineer where engineer.engineerid='" + engineerid + "'";
//        row = cnn.delete(sql);
//        return row;
        
        DBHelper.getInstance().initConnection();
        ArrayList<String> arrP= new ArrayList<String>();
        arrP.add(engineerid);
        String message = DBHelper.getInstance().executeUpdateProcReturn("Delete_Engineer", arrP);
        DBHelper.getInstance().closeConnection();   
        return message;
        //---<Hien 07/25/2011: fix code>
    }

    /**
     * Select with @param
     */
    public ArrayList<Engineer> select(String engineerid) throws SQLException {        
        ArrayList<Engineer> enList = new ArrayList<Engineer>();
        Conn cnn = new Conn();
        String sql = "select * from engineer where engineer.engineerid='" + engineerid + "'";
        ResultSet rs = cnn.exec(sql);
        while (rs.next()) {
            Engineer engineer = new Engineer();
            engineer.setEngineerID(rs.getString("EngineerID"));
            engineer.setFirstName(rs.getString("FirstName"));
            engineer.setLastName(rs.getString("LastName"));
            engineer.setMajor(rs.getString("Major"));
            engineer.setDescription(rs.getString("Description"));
            enList.add(engineer);
        }
        return enList;
    }

    /**
     * Update
     */
    public int update(String engineerid, String firstname, String lastname, String major, String description) throws SQLException {
        int row = 0;
        Conn cnn = new Conn();
        String sql = "update engineer e set e.firstname=?, e.lastname=?, e.major=?, e.description=? where e.engineerid='" + engineerid + "'";
        PreparedStatement prstm = cnn.getPrepareStatement(sql);
        //prstm.setString(1, engineerid);
        prstm.setString(1, firstname);
        prstm.setString(2, lastname);
        prstm.setString(3, major);
        prstm.setString(4, description);
        //prstm.setInt(6, 0);
        row = prstm.executeUpdate();
        return row;
    }
}
